package com.itboy.mall.pms.skuimages.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.skuimages.bo.MallSkuImagesAddBo;
import com.itboy.mall.pms.skuimages.bo.MallSkuImagesQueryBo;
import com.itboy.mall.pms.skuimages.bo.MallSkuImagesEditBo;
import com.itboy.mall.pms.skuimages.domain.MallSkuImages;
import com.itboy.mall.pms.skuimages.mapper.MallSkuImagesMapper;
import com.itboy.mall.pms.skuimages.vo.MallSkuImagesVo;
import com.itboy.mall.pms.skuimages.service.IMallSkuImagesService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * sku图片Service业务层处理
 *
 * @author itboy
 * @date 2021-08-07
 */
@Service
public class MallSkuImagesServiceImpl extends ServicePlusImpl<MallSkuImagesMapper, MallSkuImages> implements IMallSkuImagesService {

    @Override
    public MallSkuImagesVo queryById(Long id){
        return getVoById(id, MallSkuImagesVo.class);
    }

    @Override
    public TableDataInfo<MallSkuImagesVo> queryPageList(MallSkuImagesQueryBo bo) {
        PagePlus<MallSkuImages, MallSkuImagesVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallSkuImagesVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallSkuImagesVo> queryList(MallSkuImagesQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallSkuImagesVo.class);
    }

    private LambdaQueryWrapper<MallSkuImages> buildQueryWrapper(MallSkuImagesQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallSkuImages> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuId() != null, MallSkuImages::getSkuId, bo.getSkuId());
        lqw.eq(StrUtil.isNotBlank(bo.getImgUrl()), MallSkuImages::getImgUrl, bo.getImgUrl());
        lqw.eq(bo.getImgSort() != null, MallSkuImages::getImgSort, bo.getImgSort());
        lqw.eq(bo.getDefaultImg() != null, MallSkuImages::getDefaultImg, bo.getDefaultImg());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallSkuImagesAddBo bo) {
        MallSkuImages add = BeanUtil.toBean(bo, MallSkuImages.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallSkuImagesEditBo bo) {
        MallSkuImages update = BeanUtil.toBean(bo, MallSkuImages.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallSkuImages entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	@Transactional
	public Boolean batchAdd(List<MallSkuImages> bo) {
    	this.remove(new LambdaQueryWrapper<MallSkuImages>().eq(MallSkuImages::getSkuId,bo.get(0).getSkuId()));
		return this.saveBatch(bo);
	}

	@Override
	public List<MallSkuImages> getImagesBySkuId(Long skuId) {
		List<MallSkuImages> skuImagesList = this.baseMapper.selectList(new LambdaQueryWrapper<MallSkuImages>().eq(MallSkuImages::getSkuId, skuId));
		return skuImagesList;
	}
}
